//
//  WCNPiccRecordListVC.m
//  WCN
//
//  Created by wsj2015 on 16/4/1.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNPiccRecordListVC.h"
#import "WCNPICCPreserveRecordModel.h"
#import "WCNPiccExbutionRecordModel.h"
#import "WCNPiccPerserveCell.h"
#import "WCNPiccExbutionCell.h"
//#import "WCNRecordComentListVC.h"
#import "WCNNewPICCPreserveVC.h"
#import "WCNCareRecordCommentListVC.h"
#import "WCButton.h"
#import "UINavigationItem+Offset.h"
#import "PopViewLikeQQView.h"
#import "WCNNewPICCExtubationRecordVC.h"
#import "WCNPICCPreserveRecordAddVC.h"
#import "UITableViewRowAction+JZExtension.h"
#import "JCAlertView.h"
@interface WCNPiccRecordListVC ()<UIActionSheetDelegate>


@property (nonatomic,  strong) NSMutableArray *dataArray;


@property (nonatomic,  assign) NSInteger totalPage;
@property (nonatomic,  assign) BOOL is_firstAppear;

@property (nonatomic,  strong) NSIndexPath *longPressIndex;

@end

@implementation WCNPiccRecordListVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.dataArray = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     _is_firstAppear = YES;
    
    [self configUI];
    
    UILongPressGestureRecognizer *gestureLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(gestureLongPress:)];
    gestureLongPress.minimumPressDuration = 1;
    [self.tableView addGestureRecognizer:gestureLongPress];
    
}
- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    if (self.is_firstAppear) {
        [self.tableView.header beginRefreshing];
        _is_firstAppear = NO;
        
    }else {
        [self getPiccRecordList:YES];
    }

}

- (void)getPiccRecordList:(BOOL)isRecovery{
    
    self.tableView.loading = YES;
      static NSInteger page =1;
    weakSelf_define;
    if (isRecovery) {
        page = 1;
       
       
    }else{
        
        page++;
    }
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"rows"] =@(10);
    params[@"page"] = @(page);
    params[@"piccId"] = @(self.piccId);
     [[WCNetworkAccessManager sharedMg] POST:kGetPiccRecordList parameters:params success:^(NSDictionary *responseObject) {
         if (isRecovery) {
              [weakSelf.dataArray removeAllObjects];
         }
        weakSelf.totalPage = [responseObject[@"data"][@"totalPage"] integerValue];
         
        if ([responseObject[@"data"][@"ext"] isKindOfClass:[NSArray class]]) {
            if ([responseObject[@"data"][@"ext"] isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dict in responseObject[@"data"][@"ext"]) {
                    
                    WCNPiccExbutionRecordModel *model =[[WCNPiccExbutionRecordModel alloc] init];
                    
                    [model setValuesForKeysWithDictionary:dict];
                    [weakSelf.dataArray addObject:model];
                    
                }
                
            }
        
            if ([responseObject[@"data"][@"result"] isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dictsub in responseObject[@"data"][@"result"]) {
                    WCNPICCPreserveRecordModel *model =[[WCNPICCPreserveRecordModel alloc] init];;
                    [model setValuesForKeysWithDictionary:dictsub];
                    [weakSelf.dataArray addObject:model];
                }

            }
            if ((page-1)==[responseObject[@"totalPage"] integerValue]) {
                [weakSelf.tableView.footer endRefreshing];
                [weakSelf.tableView.footer noticeNoMoreData];
                
            }
            [weakSelf.tableView reloadData];
        }
        
        
    } failure:nil netError:nil always:^{
        
        if (page > 1) {
            [weakSelf.tableView.footer endRefreshing];
            if (page - 1 == weakSelf.totalPage) {
                
            [weakSelf.tableView.footer noticeNoMoreData];
            }
        }else {
            [weakSelf.tableView.header endRefreshing];
            [weakSelf.tableView.footer resetNoMoreData];
        }

        weakSelf.tableView.loading = NO;
    }];
    
}
#pragma mark -删除记录
- (void)deleteRecord{
    NSInteger  a = self.longPressIndex.row;
    WCNBaseModel * model = self.dataArray[self.longPressIndex.row];
    NSString *url;
    NSInteger recordId;
    if ([model isKindOfClass:[WCNPICCPreserveRecordModel class]]){
        url = kDelepiccMaintainRecord;
        recordId = [[model valueForKey:@"recordID"] integerValue];
    }else{
        url =kDelePiccExtubationRecord;
        recordId = [[model valueForKey:@"id"] integerValue];
    }
    
    
     weakSelf_define;
    CoreSVPLoading(@"正在删除...", NO);
    [[WCNetworkAccessManager sharedMg] POST:url parameters:@{@"id":@(recordId)} success:^(NSDictionary *responseObject) {
        CoreSVPSuccess(responseObject[@"data"]);
        [weakSelf.tableView.header beginRefreshing];
    } failure:nil netError:nil always:^{
        [CoreSVP dismiss];
    }];
    
    
  
}
- (void)configUI{
    self.navigationItem.titleView = self.titleView;
   
//    nameLabel.text = [wcde]
    self.titleLabel.text = self.patientName;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNPiccExbutionCell" bundle:nil] forCellReuseIdentifier:@"WCNPiccExbutionCell"];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64);
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNPiccPerserveCell" bundle:nil] forCellReuseIdentifier:@"WCNPiccPerserveCell"];
    defineWeakSelf;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf getPiccRecordList:YES];
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf getPiccRecordList:NO];
    }];
    [self.tableView gzwLoading:^{
        [weakSelf getPiccRecordList:YES];
    }];
    
    WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypeAdd];
    [rightBtn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem addRightBarButtonItem:rightBtn];
    
}

- (void)add{
    
    defineWeakSelf;
    CGPoint point = CGPointMake(1, 0);
    [PopViewLikeQQView configCustomPopViewWithFrame:CGRectMake(kScreenWidth-10-200, 65, 200, 200) imagesArr:@[@"CaseCell_BlueHeader",@"CaseCell_BlueHeader"] dataSourceArr:@[@"新增拔管记录",@"新增PICC维护记录"] anchorPoint:point seletedRowForIndex:^(NSInteger index) {
        if (index == 0) {
            WCNNewPICCExtubationRecordVC *vc = [[WCNNewPICCExtubationRecordVC alloc] initWithTitle:nil type:WCNNewPICCExtubationRecordTypeAdd];
            vc.patientId = self.piccId;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            
            WCNPICCPreserveRecordAddVC *vc =[[WCNPICCPreserveRecordAddVC alloc] initWithType:WCNPICCPreserveRecordTypeAdd];
            vc.piccPatientId = self.piccId;
            [weakSelf.navigationController pushViewController:vc animated:YES];
            
        }
    } animation:YES timeForCome:0.3 timeForGo:0.3];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)clickTitleView{
    
    WCNNewPICCPreserveVC *vc =[WCNNewPICCPreserveVC new];
    vc.vcType = WCNewPICCPreserveVCTypeModify;
    vc.piccRecordId = self.piccId;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     WCNBaseModel * model = self.dataArray[indexPath.row];
    UITableViewCell *idCell;
    weakSelf_define;
    if ([model isKindOfClass:[WCNPICCPreserveRecordModel class]]) {
        
      WCNPiccPerserveCell  *cell =[tableView dequeueReusableCellWithIdentifier:@"WCNPiccPerserveCell" forIndexPath:indexPath];
        cell.model = (WCNPICCPreserveRecordModel *)model;
        cell.clickBlock = ^(NSInteger piccId){
            WCNCareRecordCommentListVC *vc =[WCNCareRecordCommentListVC new];
            vc.recordId = piccId;
            vc.recordType = 3;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        idCell =cell;
    }else{
        WCNPiccExbutionCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCNPiccExbutionCell" forIndexPath:indexPath];
        cell.clickBlock = ^(NSInteger piccId){
            
            WCNCareRecordCommentListVC *vc =[WCNCareRecordCommentListVC new];
            vc.recordId = piccId;
            vc.recordType = 2;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
//
        cell.model = (WCNPiccExbutionRecordModel *)model;
        idCell = cell;
    }
    idCell.selectionStyle = UITableViewCellSelectionStyleGray;
    return idCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNBaseModel *model = self.dataArray[indexPath.row];
    
    if ([model isKindOfClass:[WCNPICCPreserveRecordModel class]]) {
        return 139;
    }else{
        return 112;
    }
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WCNBaseModel *model = self.dataArray[indexPath.row];
    if ([model isKindOfClass:[WCNPICCPreserveRecordModel class]]) {
        WCNPICCPreserveRecordAddVC *vc =[[WCNPICCPreserveRecordAddVC alloc] initWithType:WCNPICCPreserveRecordTypeScan];
        vc.piccId = [[model valueForKey:@"recordID"] integerValue];
        [self.navigationController pushViewController:vc animated:YES];

    }else{
        WCNNewPICCExtubationRecordVC *vc = [[WCNNewPICCExtubationRecordVC alloc] initWithTitle:nil type:WCNNewPICCExtubationRecordTypeScan];
        vc.piccRecordId = [[model valueForKey:@"id"] integerValue];
        [self.navigationController pushViewController:vc animated:YES];

        
    }
    
}
#pragma mark - 长按显示提示
- (void)gestureLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint tmpPointTouch = [gestureRecognizer locationInView:self.tableView];
    if (gestureRecognizer.state ==UIGestureRecognizerStateBegan) {
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:tmpPointTouch];
        if (indexPath == nil) {
            NSLog(@"not tableView");
        }else{
            self.longPressIndex = [NSIndexPath indexPathForRow:indexPath.row  inSection:0];
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"修改", @"删除", nil];
            [actionSheet showInView:self.view];
            
        }
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
     defineWeakSelf;
   if (buttonIndex == 0) {
        WCNBaseModel *model = self.dataArray[self.longPressIndex.row];
       
        if ([model isKindOfClass:[WCNPiccExbutionRecordModel class]]) {
            WCNNewPICCExtubationRecordVC *vc = [[WCNNewPICCExtubationRecordVC alloc] initWithTitle:nil type:WCNNewPICCExtubationRecordTypeEdit];
            vc.piccRecordId = [[model valueForKey:@"id"] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else{
            
            WCNPICCPreserveRecordAddVC *vc =[[WCNPICCPreserveRecordAddVC alloc] initWithType:WCNPICCPreserveRecordTypeEdit];
            vc.piccId = [[model valueForKey:@"recordID"] integerValue];
            [weakSelf.navigationController pushViewController:vc animated:YES];
            
        }
       
       
    }else if (buttonIndex == 1) {
        
    [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"师父确认删除" ButtonType:1 ButtonTitle:@"否" Click:nil ButtonType:2 ButtonTitle:@"确认删除" Click:^{
         [weakSelf deleteRecord];
    }];
        
      
        
    }
    
    
}



@end
